import dcmjs from 'dcmjs';
export function getInformationOfBuffer(arrayBuffer: any, attr?: string) {
	const { DicomMetaDictionary, DicomMessage } = dcmjs.data;
	const dicomData = DicomMessage.readFile(arrayBuffer);
	const dataset = DicomMetaDictionary.namifyDataset(dicomData.dict);

	const StudyID = dataset.StudyID ? dataset.StudyID.Value[0] : '-';
	const Modality = dataset.Modality ? dataset.Modality.Value[0] : '-';
	const PatientSex = dataset.PatientSex ? dataset.PatientSex.Value[0] : '-';
	const PatientAge = dataset.PatientAge ? dataset.PatientAge.Value[0] : '-';
	const StudyDate = dataset.StudyDate ? dataset.StudyDate.Value[0] : '-';
	const ImagerPixelSpacing = dataset.ImagerPixelSpacing?.Value;
	const ImagePositionPatient = dataset.ImagePositionPatient?.Value;
	const ImageOrientationPatient = dataset.ImageOrientationPatient?.Value;
	const PixelSpacing = dataset.PixelSpacing?.Value

	// console.log('dataset', dataset);

	const information = {
		StudyID,
		Modality,
		PatientSex,
		PatientAge,
		StudyDate,
		ImagerPixelSpacing,
		ImagePositionPatient,
		ImageOrientationPatient,
		PixelSpacing
	};

	// console.log('information',information)

	//namifyDataset //naturalizeDataset
	if (attr) {
		return dataset[attr];
	}
	return { information, tags: dataset };
}

export function getInformationOfImage(
	image: any,
	attr?: string
): {
	information: { StudyID: string; Modality: string; PatientSex: string; PatientAge: string; StudyDate: string; ImagerPixelSpacing: Array<string>; ImagePositionPatient: Array<string>,ImageOrientationPatient:Array<string>,PixelSpacing:Array<string> };
	tags: any;
} {
	return getInformationOfBuffer(image.data.byteArray.buffer, attr);
}
